package com.songtech.uitest.module.propose;

import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.AlertHandler;
import com.songtech.uitest.module.common.MenuHandler;
import com.songtech.uitest.module.common.TabHandler;
import com.songtech.uitest.resources.IndentResource;
import com.songtech.uitest.util.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class AddTower {

    @Autowired
    @Qualifier("indentResource")
    private IndentResource indentResource;

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private AlertHandler alertHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(AddTower.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    /**
     *
     * @param step 类似"00"
     * @throws Exception
     */
    public void addOneTower(String step) throws Exception{


        WebDriver driver = stepParser.parseDriver(step);
        AutoWait.setWebDriverWait(driver);


        //打开模拟页
        menuHandler.clickMenuItem(driver, "铁塔建设", "需求提出", "");

        AutoWait.jumpIntoFrameCheckLoading(By.name("iframeundefined"), 0);

        //点击单个录入中的铁塔类
        WebElement btn = AutoWait.findByCss("button.btn.btn-default.dropdown-toggle.btn-primary");
        btn.click();

        WebElement ul = AutoWait.findByXpath(btn, "following-sibling::ul[1]");
        AutoWait.findByXpath(ul, ".//a[1]").click();

        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);

        //回到需求新增页上
        AutoWait.jumpIntoFrameCheckLoading(By.name("iframe0"), 2);



        logger.info(space.getSpace() + "开始填入新铁塔需求信息");
        //填入表单数据
        Select prjBatch = new Select(driver.findElement(By.id("projectBatch")));
        prjBatch.selectByIndex(0);
        Thread.sleep(1000);

        Select prjName = new Select(driver.findElement(By.id("projectNameId")));
        prjName.selectByIndex(1);

        Select region = new Select(driver.findElement(By.id("region")));
        region.selectByVisibleText(indentResource.getProposeRegion());
        Thread.sleep(1500);

        Select country = new Select(driver.findElement(By.id("county")));
        country.selectByVisibleText(indentResource.getProposeCountry());

        Select priority = new Select(driver.findElement(By.id("priority")));
        priority.selectByVisibleText(indentResource.getProposePriority());

        String randomID = RandomID.next();
        WebElement siteAddress = driver.findElement(By.id("siteAddress"));
        siteAddress.sendKeys("随机站点" + randomID);

        WebElement longitude = driver.findElement(By.id("longitude"));
        longitude.sendKeys(indentResource.getProposeLongitude());

        WebElement latitude = driver.findElement(By.id("latitude"));
        latitude.sendKeys(indentResource.getProposeLatitude());


        WebElement coverRange = driver.findElement(By.id("coverRange"));
        coverRange.sendKeys("较大覆盖区域");

        WebElement offsetRadii = driver.findElement(By.id("offsetRadii"));
        offsetRadii.sendKeys("11");

        WebElement antennaHeightIn = driver.findElement(By.id("antennaHeightIn"));
        antennaHeightIn.sendKeys("13");

        WebElement antennaHeightEnd = driver.findElement(By.id("antennaHeightEnd"));
        antennaHeightEnd.sendKeys("15");

        WebElement systemCount = driver.findElement(By.id("systemCount"));
        systemCount.sendKeys("1");

        //信源频段
        WebElement bt = driver.findElement(By.cssSelector("button.btn.dropdown-toggle.btn-default.bs-placeholder"));
        bt.click();
        Thread.sleep(500);
        WebElement dropDiv = driver.findElement(By.cssSelector("div.dropdown-menu.open"));
        String[] ranges = indentResource.getProposeSignalRangesArray();
        for(String range : ranges){
            if(range.substring(0,2).equals("5G")){
                String s = range.substring(3); //取到的值为：3.5GHz,4.9GHz
                dropDiv.findElement(By.xpath("./ul//li/a//span[contains(text(),'" + s + "')]/..")).click();
            }
            else {
                dropDiv.findElement(By.xpath("./ul//li/a//span[text()='" + range + "']/..")).click();
            }
        }
        Thread.sleep(500);
        bt.click();


        WebElement antennaCount = driver.findElement(By.id("antennaCount"));
        antennaCount.sendKeys("3");

        WebElement deliverTime = driver.findElement(By.cssSelector("span.glyphicon.glyphicon-calendar"));
        deliverTime.click();

        //需求来源细分种类，即建设类型
        Select demandResourceType = new Select(driver.findElement(By.id("demandResourceType")));
        if(indentResource.getProposeDemandResourceType().equals("新址新建站")){
            demandResourceType.selectByIndex(0);
            indent.setCurrentBuildType("新址新建站"); //设置单子当前建设类型
        }
        else{
            demandResourceType.selectByIndex(1);
            indent.setCurrentBuildType("共址新建站"); //设置单子当前建设类型
        }


        WebElement planningNumber = driver.findElement(By.id("planningNumber"));
        planningNumber.sendKeys(randomID);

        WebElement remark = driver.findElement(By.id("remark"));
        remark.sendKeys("新建" + randomID + "站点时的留言信息");

        //保存草稿
        WebElement saveSet = driver.findElement(By.id("saveSet"));
        saveSet.click();
        Thread.sleep(3000);

        //点击提示框的关闭按钮
        alertHandler.checkAndGo(driver);

        Thread.sleep(2000);
        logger.info(space.getSpace() + "新需求草稿已成功保存");


        indent.setStepPosition(indent.getStepPosition() + 1);


    }



    /**
     * 三方需求提出录入
     * @param step 类似"00"
     * @throws Exception
     */
    public void add3OneTower(String step) throws Exception {

        WebDriver driver = stepParser.parseDriver(step);
        AutoWait.setWebDriverWait(driver);

        tabHandler.closeAllTabs(driver);
        //先打开三方建设-需求提出
        //菜单打开三方需求提出页面
        menuHandler.clickMenuItem(driver, "三方建设", "需求提出", "");

        AutoWait.jumpIntoFrameCheckLoading(By.name("iframeundefined"), 0);

        AutoWait.findByID("commit-btn").click();

        driver.switchTo().defaultContent();

        //跳到打开的录入页面
        AutoWait.jumpIntoFrameCheckLoading(By.name("iframe0"), 2);


        //查询此单
        driver.findElement(By.id("keywords")).sendKeys((indent.getMobileId()));

        driver.findElement(By.xpath("//button[text()='查询']")).click();

        AutoWait.loadingBarBGDieAway();

        //搜索并进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='确认选择需求单']")).click();

        //提交情况提示框出现,关掉它
        alertHandler.checkAndGo(driver);


        logger.info(space.getSpace() + "三方需求录入成功");


        indent.setStepPosition(indent.getStepPosition() + 1);

    }
}
